package com.central.apps.controller;

import cn.hutool.core.util.CharUtil;
import com.alibaba.fastjson.JSONObject;
import com.central.app.common.constant.ShowTypeEnum;
import com.central.app.common.constant.SwitchEnum;
import com.central.app.entity.BizJsonAttribute;
import com.central.app.service.AnalysisPageJsonService;
import com.central.app.service.HandlerService;
import com.central.apps.common.util.DateUtiles;
import com.central.common.web.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "部署接口")
@RestController
@RequestMapping("/deploy")
public class DeployController {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private AnalysisPageJsonService analysisPageJsonService;
    @Autowired
    private DataSource dataSource;

    @PostMapping("/generateIncrement")
    public Result generateIncrement(@RequestBody Map<String, Object> params) throws Exception {
        Integer dcId = MapUtils.getInteger(params, "dcId");
        String filePath = MapUtils.getString(params, "filePath");
        if (dcId == null) {
            return Result.failed("未传递发版配置主键ID!");
        }

        //文件夹加上日期
        String dateStr = DateUtiles.getYmdNew(new Date());
        filePath = new StringBuilder(filePath).append("/" + dateStr).toString();
        File folderPath = new File(filePath);

        // 如果文件夹不存在
        if (!folderPath.exists()) {
            // 创建文件夹
            folderPath.mkdir();
        } else {
            // 清空文件夹
//            deleteFolders(filePath);
        }

        // 查询发版应用配置明细信息
        List<Map<String, Object>> list = handlerService.findBySql("select appid, appnum, appdesc, iscontaindict, iscontainflow, iscontainclass from gq_dcappcfgdetail where dcid = " + dcId);

        if (CollectionUtils.isEmpty(list)) {
            return Result.failed("未查询到应用配置明细信息!");
        }

        // 循环生成增量文件
        Connection connection = null;
        Statement statement = null;

        // 数据库连接信息
        try {
            // 连接数据库
            connection = dataSource.getConnection();

            // 创建Statement对象
            statement = connection.createStatement();

            for (Map<String, Object> map : list) {
                Integer appId = MapUtils.getInteger(map, "appid");
                String appDesc = MapUtils.getString(map, "appdesc");
                String isContainDict = MapUtils.getString(map, "iscontaindict");
                String isContainFlow = MapUtils.getString(map, "iscontainflow");
                String isContainClass = MapUtils.getString(map, "iscontainclass");

                FileWriter fileWriter = null;
                try {
                    // 生成SQL文件
                    String fileName = appDesc + ".sql";
                    StringBuilder stb = new StringBuilder();
                    StringBuilder targetFilePath = stb.append(filePath).append("/").append(fileName);
                    File file = new File(targetFilePath.toString());

                    fileWriter = new FileWriter(file);
                    //定义输出文件编码格式，防止同步数据乱码
                    fileWriter.write("SET NAMES utf8;");
                    fileWriter.write(CharUtil.LF);

                    // 生成基础信息SQL
                    generateBaseInfo(statement, fileWriter, appId);

                    // 生成数据字典SQL
                    if (SwitchEnum.YES.getCode().equals(isContainDict)) {
                        dealPageDict(statement, fileWriter, appId);
                    }

                    // 生成工作流SQL
                    if (SwitchEnum.YES.getCode().equals(isContainFlow)) {
                        dealFlow(statement, fileWriter, appId);
                    }

                    // TODO 生成类文件目录
                    if (SwitchEnum.YES.getCode().equals(isContainClass)) {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fileWriter != null) {
                        fileWriter.close();
                    }
                }
            }

            // 生成报表SQL文件
            List<Map<String, Object>> reportList = handlerService.findBySql("select reportname from gq_dcreportcfgdetail where dcid = " + dcId);
            if (CollectionUtils.isNotEmpty(reportList)) {
                // 生成SQL文件
                String fileName = "报表.sql";
                StringBuilder stb = new StringBuilder();
                StringBuilder targetFilePath = stb.append(filePath).append("/").append(fileName);
                File file = new File(targetFilePath.toString());

                FileWriter fileWriter = null;
                try {
                    fileWriter = new FileWriter(file);
                    //定义输出文件编码格式，防止同步数据乱码
                    fileWriter.write("SET NAMES utf8;");
                    fileWriter.write(CharUtil.LF);

                    for (Map<String, Object> reportMap : reportList) {
                        String reportName = MapUtils.getString(reportMap, "reportname");

                        // 1.报表文件表
                        Map<String, String> paramMap = new HashMap<>(3);
                        paramMap.put("tableName", "sys_ureport_file");
                        paramMap.put("promptInfo", "#处理报表文件表:sys_ureport_file");
                        paramMap.put("queryCond", " where name = '" + reportName + "'");

                        commonGenerateInfo(statement, fileWriter, paramMap);

                        // 2.报表配置表
                        paramMap = new HashMap<>(4);
                        paramMap.put("tableName", "sys_report");
                        paramMap.put("promptInfo", "#处理报表配置表:sys_report");
                        paramMap.put("queryCond", " where file = '" + reportName + "'");
                        paramMap.put("returnFieldName", "id");

                        String reportId = commonGenerateInfo(statement, fileWriter, paramMap);

                        // 3.报表配置参数表
                        paramMap = new HashMap<>(3);
                        paramMap.put("tableName", "sys_report_param");
                        paramMap.put("promptInfo", "#处理报表配置参数表:sys_report_param");
                        paramMap.put("queryCond", " where report_id = " + reportId);

                        commonGenerateInfo(statement, fileWriter, paramMap);

                        // 表单项
                        // 1.下拉框
                        paramMap = new HashMap<>(3);
                        paramMap.put("tableName", "sys_dict");
                        paramMap.put("promptInfo", "#处理表单项下拉框:sys_dict");
                        paramMap.put("queryCond", " where code in (select formdictionaryCode from gq_applyformitem where formnum in (select default_val from sys_report_param where type = 4 and report_id = " + reportId + ")) " +
                                " or pid = (select id from sys_dict where code in (select formdictionaryCode from gq_applyformitem where formnum in (select default_val from sys_report_param where type = 4 and report_id = " + reportId + "))) ");

                        commonGenerateInfo(statement, fileWriter, paramMap);

                        // 2.弹出框
                        // 2.1.表单项主表
                        paramMap = new HashMap<>(3);
                        paramMap.put("tableName", "gq_applyformitem");
                        paramMap.put("promptInfo", "#处理表单项主表:gq_applyformitem");
                        paramMap.put("queryCond", " where formnum in (select default_val from sys_report_param where type = 3 and report_id = " + reportId + ")");

                        commonGenerateInfo(statement, fileWriter, paramMap);

                        // 2.2.表单项查询条件配置表
                        paramMap = new HashMap<>(3);
                        paramMap.put("tableName", "gq_searchitemsetting");
                        paramMap.put("promptInfo", "#处理表单项查询条件配置表:gq_searchitemsetting");
                        paramMap.put("queryCond", " where formnum in (select default_val from sys_report_param where type = 3 and report_id = " + reportId + ")");

                        commonGenerateInfo(statement, fileWriter, paramMap);

                        // 2.3.表单项弹出框字段配置表
                        paramMap = new HashMap<>(3);
                        paramMap.put("tableName", "gq_inputbtnfieldsetting");
                        paramMap.put("promptInfo", "#处理表单项弹出框字段配置表:gq_inputbtnfieldsetting");
                        paramMap.put("queryCond", " where formnum in (select default_val from sys_report_param where type = 3 and report_id = " + reportId + ")");

                        commonGenerateInfo(statement, fileWriter, paramMap);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fileWriter != null) {
                        fileWriter.close();
                    }
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

        return Result.succeed("生成文件成功！");
    }

    @PostMapping("/generate")
    public Result generate(@RequestBody Map<String, Object> map) throws Exception {
        String projectId = MapUtils.getString(map, "projectId");
        String filePath = MapUtils.getString(map, "filePath");
        String url = MapUtils.getString(map, "url");
        String userName = MapUtils.getString(map, "userName");
        String pwd = MapUtils.getString(map, "pwd");
        if(StringUtils.isEmpty(projectId)) {
            return Result.failed("请传递项目惟一ID！");
        }

        if(StringUtils.isEmpty(filePath)) {
            filePath = "/home/deploy/demand/deploy";
        }

        File folderPath = new File(filePath);

        // 如果文件夹不存在
        if (!folderPath.exists()) {
            // 创建文件夹
            folderPath.mkdir();
        } else {
            // 清空文件夹
//            deleteFolders(filePath);
        }

        // 初始化表信息
        List<Map<String, String>> tableList = initTableInfo();

        Connection connection = null;
        Statement statement = null;

        // 数据库连接信息
        try {
            // 连接数据库
            connection = DriverManager.getConnection(url, userName, pwd);

            // 创建Statement对象
            statement = connection.createStatement();

            // 循环表信息并生成insert语句，输出到文件并保存到服务器
            for (Map<String, String> tableMap : tableList) {
                String fileName = MapUtils.getString(tableMap, "fileName");
                String tableName = MapUtils.getString(tableMap, "tableName");
                String whereCond = MapUtils.getString(tableMap, "whereCond");
                StringBuilder sql = new StringBuilder();
                sql.append("select * from ").append(tableName).append(" where ").append(whereCond.replace("@projectId", projectId));

                StringBuilder stb = new StringBuilder();
                StringBuilder targetFilePath = stb.append(filePath).append("/").append(fileName);

                File file = new File(targetFilePath.toString());
                System.out.println("file====================================="+file);
                FileWriter fileWriter = new FileWriter(file);
                //定义输出文件编码格式，防止同步数据乱码
                fileWriter.write("SET NAMES utf8;");
                fileWriter.write(CharUtil.LF);

                // 执行查询
                ResultSet resultSet = statement.executeQuery(sql.toString());

                // 获取结果集的元数据
                ResultSetMetaData metaData = resultSet.getMetaData();

                // 获取列数
                int columnCount = metaData.getColumnCount();

                // 遍历结果集
                while (resultSet.next()) {
                    // 创建StringBuilder来构建INSERT语句
                    StringBuilder insertStatement = new StringBuilder("INSERT INTO " + tableName + " (");
                    StringBuilder valuesStatement = new StringBuilder("VALUES (");

                    // 遍历每个字段
                    for (int i = 1; i <= columnCount; i++) {
                        // 获取字段名和类型
                        String columnName = metaData.getColumnName(i);
                        int columnType = metaData.getColumnType(i);
                        String stringValue = resultSet.getString(i);

                        // 根据字段类型处理值
                        switch (columnType) {
                            case Types.BIGINT:
                            case Types.DECIMAL:
                            case Types.DOUBLE:
                            case Types.FLOAT:
                            case Types.INTEGER:
                                // 添加字段名和值到INSERT语句中
                                insertStatement.append(columnName).append(",");
                                valuesStatement.append(stringValue).append(",");
                                break;
                            default:
                                // 添加字段名和值到INSERT语句中
                                insertStatement.append(columnName).append(",");

                                if (stringValue == null) {
                                    valuesStatement.append(stringValue).append(",");
                                } else {
                                    valuesStatement.append("'").append(stringValue).append("',");
                                }

                                break;
                        }
                    }

                    // 移除INSERT语句和VALUES语句末尾的逗号
                    insertStatement.deleteCharAt(insertStatement.length() - 1);
                    valuesStatement.deleteCharAt(valuesStatement.length() - 1);

                    // 添加INSERT语句的结尾
                    insertStatement.append(") ").append(valuesStatement).append(");");

                    // 输出INSERT语句
                    fileWriter.write(insertStatement.toString());
                    fileWriter.write(CharUtil.LF);
                }

                resultSet.close();
                fileWriter.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

        return Result.succeed("生成文件成功！");
    }

    /**
     * 处理应用对应工作流
     *
     * @param statement
     * @param fileWriter
     * @param appId
     * @throws Exception
     */
    private void dealFlow(Statement statement, FileWriter fileWriter, Integer appId) throws Exception {
        // 查询应用对应流程信息
        Map<String, Object> map = handlerService.findOneBySql("select flowkey from biz_app where id = " + appId);
        String flowKey = MapUtils.getString(map, "flowkey");

        if (StringUtils.isEmpty(flowKey)) {
            return;
        }

        // 1.流程模型表
        Map<String, String> paramMap = new HashMap<>(4);
        paramMap.put("tableName", "act_re_model");
        paramMap.put("promptInfo", "#处理流程模型表:act_re_model");
        paramMap.put("queryCond", " where KEY_ = '" + flowKey + "'");
        paramMap.put("returnFieldName", "ID_");

        String modelId = commonGenerateInfo(statement, fileWriter, paramMap);

        // 2.流程节点表
        paramMap = new HashMap<>(3);
        paramMap.put("tableName", "act_cus_candidate");
        paramMap.put("promptInfo", "#处理流程节点表:act_cus_candidate");
        paramMap.put("queryCond", " where modelid = '" + modelId + "'");

        commonGenerateInfo(statement, fileWriter, paramMap);

        // 3.流程节点事件表
        paramMap = new HashMap<>(3);
        paramMap.put("tableName", "act_cus_action");
        paramMap.put("promptInfo", "#处理流程节点事件表:act_cus_action");
        paramMap.put("queryCond", " where modelid = '" + modelId + "'");

        commonGenerateInfo(statement, fileWriter, paramMap);

        // 4.流程客制化表
        paramMap = new HashMap<>(4);
        paramMap.put("tableName", "act_cus_flow");
        paramMap.put("promptInfo", "#处理流程客制化表:act_cus_flow");
        paramMap.put("queryCond", " where modelid = '" + modelId + "'");
        paramMap.put("returnFieldName", "proc_def_id");

        String procDefId = commonGenerateInfo(statement, fileWriter, paramMap);

        // 5.流程部署定义表
        paramMap = new HashMap<>(4);
        paramMap.put("tableName", "act_re_procdef");
        paramMap.put("promptInfo", "#处理流程部署定义表:act_re_procdef");
        paramMap.put("queryCond", " where ID_ = '" + procDefId + "'");
        paramMap.put("returnFieldName", "DEPLOYMENT_ID");

        String deployId = commonGenerateInfo(statement, fileWriter, paramMap);

        // 6.流程部署表
        paramMap = new HashMap<>(4);
        paramMap.put("tableName", "act_re_deployment");
        paramMap.put("promptInfo", "#处理流程部署表:act_re_deployment");
        paramMap.put("queryCond", " where ID_ = '" + deployId + "'");

        commonGenerateInfo(statement, fileWriter, paramMap);
    }

    /**
     * 处理界面对应数据字典
     *
     * @param statement
     * @param fileWriter
     * @param appId
     * @throws Exception
     */
    private void dealPageDict(Statement statement, FileWriter fileWriter, Integer appId) throws Exception {
        // 解析界面json
        List<BizJsonAttribute> jsonAttributes = analysisPageJsonService.analysisJsonList(appId);

        List<String> dictCodes = new ArrayList<>();
        for (BizJsonAttribute jsonAttribute : jsonAttributes) {
            if (jsonAttribute.getType().equals(ShowTypeEnum.SELECT.getCode())) {
                // 过滤数据字典值
                String dictCode = getDictCode(jsonAttribute.getJsonString());
                if (StringUtils.isNotEmpty(dictCode) && !dictCodes.contains(dictCode)) {
                    dictCodes.add(dictCode);
                }
            }
        }

        if (CollectionUtils.isNotEmpty(dictCodes)) {
            // 转换为字符串
            String codes = dictCodes.stream().map(Object::toString).collect(Collectors.joining(","));

            // 生成数据字典SQL
            Map<String, String> paramMap = new HashMap<>(3);
            paramMap.put("tableName", "sys_dict");
            paramMap.put("promptInfo", "#处理界面对应数据字典表:sys_dict");
            paramMap.put("queryCond", " where code in (" + codes + ") or pid in (select id from sys_dict where code in (" + codes + "))");

            commonGenerateInfo(statement, fileWriter, paramMap);
        }
    }

    /**
     * 获取数据字典编码
     *
     * @param json
     * @return
     */
    private String getDictCode(String json) {
        //转化请求的 json 数据
        JSONObject jsonObject = JSONObject.parseObject(json);

        // 获取options json串
        JSONObject options = jsonObject.getJSONObject("options");

        String dictCode = options.getString("dictionaryCode");

        if (StringUtils.isNotEmpty(dictCode)) {
            return "'" + dictCode + "'";
        }

        return null;
    }

    /**
     * 通用生成信息
     *
     * @param statement
     * @param fileWriter
     * @param map
     * @throws Exception
     */
    private String commonGenerateInfo(Statement statement, FileWriter fileWriter, Map<String, String> map) throws Exception {
        String tableName = MapUtils.getString(map, "tableName");
        String promptInfo = MapUtils.getString(map, "promptInfo");
        String queryCond = MapUtils.getString(map, "queryCond");
        String returnFieldName = MapUtils.getString(map, "returnFieldName");

        fileWriter.write(promptInfo);
        fileWriter.write(CharUtil.LF);

        // 组织查询SQL
        StringBuilder querySql = new StringBuilder();
        querySql.append("select * from ").append(tableName).append(queryCond);

        // 组织删除SQL
        StringBuilder delSql = new StringBuilder();
        delSql.append("delete from ").append(tableName).append(queryCond).append(";");

        // 输出delete语句
        fileWriter.write("#删除数据");
        fileWriter.write(CharUtil.LF);
        fileWriter.write(delSql.toString());
        fileWriter.write(CharUtil.LF);

        // 输出insert语句
        fileWriter.write("#插入数据");
        fileWriter.write(CharUtil.LF);

        return writeInsertSql(statement, fileWriter, tableName, querySql.toString(), returnFieldName);
    }

    /**
     * 生成应用基础信息SQL文件
     *
     * @param statement
     * @param fileWriter
     * @param appId
     * @throws Exception
     */
    private void generateBaseInfo(Statement statement, FileWriter fileWriter, Integer appId) throws Exception {
        /*
         * #其他
         *  select * from biz_attribute_cfg;
         *  select * from biz_validate;
         *  select * from biz_version_oplog;
         *  select * from biz_view_search;
         *  select * from biz_view_search_condition;
         *  select * from biz_view_search_default;
         */

        // 1.应用表
        Map<String, String> paramMap = new HashMap<>(4);
        paramMap.put("tableName", "biz_app");
        paramMap.put("promptInfo", "#处理应用表:biz_app");
        paramMap.put("queryCond", " where id = " + appId);
        paramMap.put("returnFieldName", "object_id");

        String objectId = commonGenerateInfo(statement, fileWriter, paramMap);

        // 2.应用关联表
        paramMap = new HashMap<>(3);
        paramMap.put("tableName", "biz_relation");
        paramMap.put("promptInfo", "#处理应用关联表:biz_relation");
        paramMap.put("queryCond", " where appid = " + appId);

        commonGenerateInfo(statement, fileWriter, paramMap);

        // 3.对象表
        paramMap = new HashMap<>(3);
        paramMap.put("tableName", "biz_object");
        paramMap.put("promptInfo", "#处理对象表:biz_object");
        paramMap.put("queryCond", " where id = " + objectId);

        commonGenerateInfo(statement, fileWriter, paramMap);

        // 4.对象属性表
        paramMap = new HashMap<>(3);
        paramMap.put("tableName", "biz_attribute");
        paramMap.put("promptInfo", "#处理对象属性表:biz_object");
        paramMap.put("queryCond", " where object_id = " + objectId);

        commonGenerateInfo(statement, fileWriter, paramMap);
    }

    /**
     * 写插入SQL
     *
     * @param statement
     * @param fileWriter
     * @param tableName
     * @param sql
     * @param returnFieldName
     * @throws Exception
     */
    private String writeInsertSql(Statement statement, FileWriter fileWriter, String tableName, String sql, String returnFieldName) throws Exception {
        String resultVal = "";

        // 执行查询
        ResultSet resultSet = statement.executeQuery(sql);

        // 获取结果集的元数据
        ResultSetMetaData metaData = resultSet.getMetaData();

        // 获取列数
        int columnCount = metaData.getColumnCount();

        // 遍历结果集
        while (resultSet.next()) {
            // 创建StringBuilder来构建INSERT语句
            StringBuilder insertStatement = new StringBuilder("INSERT INTO " + tableName + " (");
            StringBuilder valuesStatement = new StringBuilder("VALUES (");

            // 遍历每个字段
            for (int i = 1; i <= columnCount; i++) {
                // 获取字段名和类型
                String columnName = metaData.getColumnName(i);
                int columnType = metaData.getColumnType(i);
                String stringValue = resultSet.getString(i);

                if (StringUtils.isNotEmpty(returnFieldName) && returnFieldName.equals(columnName)) {
                    resultVal = stringValue;
                }

                // 根据字段类型处理值
                switch (columnType) {
                    case Types.BIGINT:
                    case Types.DECIMAL:
                    case Types.DOUBLE:
                    case Types.FLOAT:
                    case Types.INTEGER:
                        // 添加字段名和值到INSERT语句中
                        insertStatement.append(columnName).append(",");
                        valuesStatement.append(stringValue).append(",");
                        break;
                    default:
                        // 添加字段名和值到INSERT语句中
                        insertStatement.append(columnName).append(",");

                        if (stringValue == null) {
                            valuesStatement.append(stringValue).append(",");
                        } else {
                            valuesStatement.append("'").append(stringValue).append("',");
                        }

                        break;
                }
            }

            // 移除INSERT语句和VALUES语句末尾的逗号
            insertStatement.deleteCharAt(insertStatement.length() - 1);
            valuesStatement.deleteCharAt(valuesStatement.length() - 1);

            // 添加INSERT语句的结尾
            insertStatement.append(") ").append(valuesStatement).append(");");

            // 输出INSERT语句
            fileWriter.write(insertStatement.toString());
            fileWriter.write(CharUtil.LF);
        }

        resultSet.close();
        return resultVal;
    }

    /**
     * 组织表相关信息
     *
     * @return
     */
    private List<Map<String, String>> initTableInfo() {
        List<Map<String, String>> tableList = new ArrayList<>();
        int i = 1;
        Map<String, String> map = new HashMap<>(3);
//        map.put("fileName", i + "项目表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_project");
        map.put("whereCond", "id = '@projectId'");
        tableList.add(map);
        i++;

        String commonCond = "projectid = '@projectId'";
        map = new HashMap<>(3);
//        map.put("fileName",  i + "对象表.sql");
        map.put("fileName",  i + ".sql");
        map.put("tableName", "tb_object");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "对象关联表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_relation");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "用户表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_user");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "项目用户表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_pro_user");
        map.put("whereCond", "project_id = '@projectId'");
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "部门表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_dept");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "数据字典表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_dict");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "模块表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_module");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "编码表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_number");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "编码前缀表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_number_prefix");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "编码序号表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_number_serialno");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "问题表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_problem");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "角色表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_role");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "角色需求按钮表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_role_demand_button");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "角色部门表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_role_dept");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "角色模块表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_role_module");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "角色用户表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_role_user");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "跟踪日志表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_trace_log");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "需求表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_demand");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "属性表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_attribute");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "需求属性组表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_demand_attrgroup");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "需求属性组关联表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_demand_attrgrouprel");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "需求复制表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_demand_copy");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "属性来源表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_attr_source");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "属性来源条件设置表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_attr_source_consetting");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "属性来源参数列设置表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_attr_source_paracolsetting");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "属性对应页面设置表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_attribute_refpagesetting");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        map = new HashMap<>(3);
//        map.put("fileName", i + "按钮表.sql");
        map.put("fileName", i + ".sql");
        map.put("tableName", "tb_button");
        map.put("whereCond", commonCond);
        tableList.add(map);
        i++;

        return tableList;
    }

    /**
     * 删除指定文件夹下文件
     *
     * @param filePath
     */
    public static void deleteFolders(String filePath) {

        Path path = Paths.get(filePath);
        try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir,
                                                          IOException exc) throws IOException {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
